package com.atguigu.ggkt.order.service.impl;

import com.atguigu.ggkt.order.service.WXPayService;
import com.atguigu.ggkt.utils.HttpClientUtils;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WXPayServiceImpl implements WXPayService {

    //微信支付方法【调用微信的接口，传入数据即可】
    @Override
    public Map<String, Object> createJsapi(String orderNo) throws Exception {
        //封装微信支付需要的参数，使用map集合
        HashMap<String, String> paramMap = new HashMap<>();
        //1、设置参数
        paramMap.put("appid", "wxf913bfa3a2c7eeeb");
        paramMap.put("mch_id", "1481962542");
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
        paramMap.put("body", "test");
        paramMap.put("out_trade_no", orderNo);
        paramMap.put("total_fee", "1");
        paramMap.put("spbill_create_ip", "127.0.0.1");
        paramMap.put("notify_url", "http://glkt.atguigu.cn/api/order/wxPay/notify");
        paramMap.put("trade_type", "JSAPI");
        paramMap.put("openid", "oQTXC56lAy3xMOCkKCImHtHoLL");

        //2、HTTPClient来根据URL访问第三方接口并且传递参数
        //通过httpclient调用微信支付接口
        HttpClientUtils client =
                new HttpClientUtils("https://api.mch.weixin.qq.com/pay/unifiedorder");

        //client设置参数【map转为xml格式】
        client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, "MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9"));
        client.setHttps(true);
        client.post();
        //3、返回第三方的数据【xml转为map集合】
        String xml = client.getContent();
        Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
        if(null != resultMap.get("result_code")  && !"SUCCESS".equals(resultMap.get("result_code"))) {
            System.out.println("error1");
        }

        //4、再次封装参数
        Map<String, String> parameterMap = new HashMap<>();
        String prepayId = String.valueOf(resultMap.get("prepay_id"));
        String packages = "prepay_id=" + prepayId;
        parameterMap.put("appId", "wxf913bfa3a2c7eeeb");
        parameterMap.put("nonceStr", resultMap.get("nonce_str"));
        parameterMap.put("package", packages);
        parameterMap.put("signType", "MD5");
        parameterMap.put("timeStamp", String.valueOf(new Date().getTime()));
        //微信支付接口返回数据
        String sign = WXPayUtil.generateSignature(parameterMap, "MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9");

        //返回结果
        //进行封装，最终返回
        Map<String, Object> result = new HashMap();
        result.put("appId", "wxf913bfa3a2c7eeeb");
        result.put("timeStamp", parameterMap.get("timeStamp"));
        result.put("nonceStr", parameterMap.get("nonceStr"));
        result.put("signType", "MD5");
        result.put("paySign", sign);
        result.put("package", packages);
        System.out.println(result);
        return result;



    }

    //根据订单号调用微信接口查询支付状态
    @Override
    public Map<String, String> queryPayStatus(String orderNo) throws Exception {
        //使用map
        Map paramMap = new HashMap<>();
        paramMap.put("appid", "wxf913bfa3a2c7eeb");//正式号
        paramMap.put("mch_id", "1481962542");//商务号
        paramMap.put("out_trade_no", orderNo);
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
        //调用http
        HttpClientUtils client =
                new HttpClientUtils("https://api.mch.weixin.qq.com/pay/orderquery");
        client.setXmlParam(
                WXPayUtil.generateSignedXml(paramMap, "MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9"));
        client.setHttps(true);
        client.post();
        //封装
        String xml = client.getContent();
        System.out.println("xml="+xml);
        Map<String, String> resMap = WXPayUtil.xmlToMap(xml);
        return resMap;
    }
}
